<template>
  <el-card>
    <el-form :inline="true" :model="queryForm" @keyup.enter="getDataList()">
      <el-form-item>
        <el-input v-model="queryForm.tableName" placeholder="表名"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="importHandle()">导入</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="success" @click="downloadBatchHandle()">生成代码</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="danger" @click="deleteBatchHandle()">删除</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="dataList" border style="width: 100%" @selection-change="selectionChangeHandle">
      <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
      <el-table-column prop="tableName" label="表名" header-align="center" align="center"></el-table-column>
      <el-table-column prop="tableComment" label="表说明" header-align="center" align="center"></el-table-column>
      <el-table-column prop="className" label="类名" header-align="center" align="center"></el-table-column>
      <el-table-column label="操作" fixed="right" header-align="center" align="center" width="300">
        <template #default="scope">
          <el-button type="primary" link @click="editHandle(scope.row.id)">编辑</el-button>
          <el-button type="primary" link @click="generatorHandle(scope.row.id)">生成代码</el-button>
          <el-button type="primary" link @click="previewHandle(scope.row.id)">预览</el-button>
          <el-button type="primary" link @click="deleteBatchHandle(scope.row.id)">删除</el-button>
          <el-button type="primary" link @click="syncHandle(scope.row)">同步</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
        :current-page="page"
        :page-sizes="pageSizes"
        :page-size="queryParams.pageSize"
        :total="total"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="sizeChangeHandle"
        @current-change="currentChangeHandle"
    >
    </el-pagination>

    <import ref="importRef" @refresh-data-list="getDataList"></import>
    <edit ref="editRef" @refresh-data-list="getDataList"></edit>
    <generator ref="generatorRef" @refresh-data-list="getDataList"></generator>
    <preview ref="preview"></preview>
  </el-card>
</template>

<script setup>
import {reactive, ref, toRefs} from 'vue'
import Import from './import.vue'
import Edit from './edit.vue'
import Generator from './generator.vue'
import Preview from './preview.vue'
import {ElMessage, ElMessageBox} from 'element-plus'
import {useTableDeleteApi, useTableListApi, useTableSyncApi} from '@/api/generator/table'
import {useDownloadApi,} from '@/api/generator/generator'

const {proxy} = getCurrentInstance();

const importRef = ref()
const editRef = ref()
const generatorRef = ref()
const preview = ref()

const queryForm = ref({})
const loading = ref(false)
const dataList = ref([])
const page = ref(0)
const pageSizes = ref([1, 10, 20, 50, 100, 200])
const limit = ref(0)
const total = ref(0)
const dataListSelections = ref([])
const ids = ref([])

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
  },
  rules: {}
});

const {queryParams, form, rules} = toRefs(data);

function getDataList() {
  useTableListApi(data.queryParams).then(response => {
    dataList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

const importHandle = (id) => {
  importRef.value.init(id)
}

const editHandle = (id) => {
  editRef.value.init(id)
}

const generatorHandle = (id) => {
  generatorRef.value.init(id)
}

const previewHandle = (id) => {
  preview.value.init(id)
}

const downloadBatchHandle = () => {
  const tableIds = dataListSelections ? dataListSelections : []

  if (tableIds.length === 0) {
    ElMessage.warning('请选择生成代码的表')
    return
  }

  useDownloadApi(tableIds)
}

const syncHandle = (row) => {
  ElMessageBox.confirm(`确定同步数据表${row.tableName}吗?`, '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    useTableSyncApi(row.id).then(() => {
      ElMessage.success('同步成功')
    })
  }).catch(() => {
  })
}

function deleteBatchHandle(id) {
  let _ids = [id] || ids.value
  useTableDeleteApi(_ids).then(response => {
    proxy.$modal.msgSuccess(response.msg)
    getDataList();
  })
}

function selectionChangeHandle(selections) {
  dataListSelections.value = selections.map((item) => item['id'])
}

function sizeChangeHandle(num) {
  data.queryParams.pageSize = num
  getDataList()
}

function currentChangeHandle(num) {
  data.queryParams.pageNum = num
  getDataList()
}

getDataList();
</script>
